/*PFSo7vFSif6TPS9ylElSOLsTYZWRjvVkdX2R2NBvWo9G2BEjtP607ol1PVvqUibo*/
/*
 * @Descripttion:
 * @version:
 * @Author: Chendch
 * @Date: 2021-06-09 10:26:49
 * @LastEditors: Chendch
 * @LastEditTime: 2021-10-19 10:37:14
 */
/******************************************************
 * 树操作工具类
 *
 ******************************************************/

/*************************************************************
 * 检查某节点下如果没有子节点，去掉children属性，设置isleaf为true（即去掉“>”符号）
 *
 * 常用于树节点移动后检查原父节点有无子节点操作
 * @param tree
 * @param pk
 * @returns {boolean}
 *************************************************************/
const checkHasChildren = (tree,pk)=> {
    if(!!!tree){
        //树组件没有数据
        return false;
    }
    let i = tree.length;
    while (i--){
        let data = tree[i];
        if (data.refpk === pk) {
            if(data.hasOwnProperty('children') && data.children.length === 0){
                delete data.children;
                data.isleaf = true;
            }
            return true;
        } else {
            if (data.hasOwnProperty('children')) {
                let res = checkHasChildren(data.children, pk);
                if(res){
                    return false
                }
            }
        }
    }
};

/*************************************************************
 * 处理树节点数据 去掉数据对象中children属性为空的children属性
 *
 * 用在加载树节点数据回调时
 * @param data
 * @returns {*}
 *************************************************************/
const dealTreeData = (data)=>{
    let deleteDataChildrenProp = function(node){
        node.iconBox = {
            addIcon:false,
            editIcon:false,
            delIcon:false
        };
        if(!node.children || node.children.length == 0) {

            delete node.children;
        }
        else{
            node.isLeaf = false;
            node.children.forEach( (e) => {
                deleteDataChildrenProp(e);
            } );
        }
    };
    data.forEach( (e) => {
        deleteDataChildrenProp(e);
    });
    return data;
};

//将一维数组改成树结构数据，并按key值排序
const createTreeData = (data, sortKey="") => {
    if (!Array.isArray(data)) {
      console.warn("createTreeData方法，参数应该为数组");
      return false;
    }
    let group = new Map();
    data.forEach(e => {
        if (!(e.values.pid && e.values.pid.value)) {
            e.values.pid = { value: PID };
        }
        group.get(e.values.pid.value)
            ? group.get(e.values.pid.value).push(e)
            : group.set(e.values.pid.value, [e]);
    });
    console.log("22--", group)

    if(!!sortKey) {
        for(let [k, v] of group.entries()) {
            v = v.sort(function(a, b) {
              return b.values[sortKey].value - a.values[sortKey].value
            })
        }
        console.log("11--", group)
    }

    function makeDOM(pid) {
        if (group.get(pid)) {
            return group.get(pid).map(e => {
                e.children = makeDOM(e.values.refpk.value);
                return e;
            });
        } else {
            return null;
        }
    }
    let newData = makeDOM(PID) || [];
    /* 添加isleaf字段 */
    const addIsLeaf = data => {
        data.forEach(item => {
            if (item.checked) {
                item.checked = true;
            } else {
                item.checked = false;
            }
            if (Array.isArray(item.children)) {
                item.isleaf = false;
                addIsLeaf(item.children);
            } else {
                item.isleaf = true;
            }
        });
    };

    addIsLeaf(newData);

    return newData;
  }

/**
 * 展开一级树节点
* @param res tree数据(平行)
* @param id tree组件id
* @param that this
* @return
*/
const expandFirstNode = (treeData, id, that, limit = null) => {
    if(!treeData || !id || !that) return ;

    let pks = [];
    for(let index = 0; index<treeData.length; index++) {
        let item = treeData[index];
        if(!!limit && index > limit) break;
        if(item && item.key) {
            pks.push(item.key);
        }
    }
    that.props.syncTree.openNodeByPk(id, pks)
    return ;
}

/**
 * 增加树虚拟跟节点
* @param params 跟节点参数
* @param childrens 叶子节点
* @return
*/
function addVirtualRootNode(params, childrens) {
    return  [
        {
            isleaf: false,
            key: "~",
            title: params.title || "root",
            id: "~",
            innercode: "~",
            pid: "__root__",
            refname: params.title || "root",
            refpk: "~",
            children: childrens || []
        }
    ]
}

export {
    checkHasChildren,
    dealTreeData,
    createTreeData,
    addVirtualRootNode,
    expandFirstNode
}
/*PFSo7vFSif6TPS9ylElSOLsTYZWRjvVkdX2R2NBvWo9G2BEjtP607ol1PVvqUibo*/
